package com.mask.im.business.controller.manage;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.mask.im.business.annotation.OptLog;
import com.mask.im.business.model.dto.LabelOptionDTO;
import com.mask.im.business.model.dto.ResourceDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.model.vo.ResourceVO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.SysResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.mask.im.business.common.constant.OptTypeConstant.DELETE;
import static com.mask.im.business.common.constant.OptTypeConstant.SAVE_OR_UPDATE;

/**
 * 资源模块
 *
 * @author jian
 */
@Api(tags = "资源模块")
@RestController
public class SysResourceController extends ApiController {

    @Autowired
    private SysResourceService sysResourceService;

    @ApiOperation(value = "查看资源列表")
    @GetMapping("/admin/resources")
    public RestResponse<List<ResourceDTO>> listResources(ConditionVO conditionVO) {
        return RestResponse.ok(sysResourceService.listResources(conditionVO));
    }

    @OptLog(optType = DELETE)
    @ApiOperation(value = "删除资源")
    @DeleteMapping("/admin/resources/{resourceId}")
    public RestResponse<?> deleteResource(@PathVariable("resourceId") Integer resourceId) {
        sysResourceService.deleteResource(resourceId);
        return RestResponse.ok();
    }

    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "新增或修改资源")
    @PostMapping("/admin/resources")
    public RestResponse<?> saveOrUpdateResource(@RequestBody @Valid ResourceVO resourceVO) {
        sysResourceService.saveOrUpdateResource(resourceVO);
        return RestResponse.ok();
    }

    @ApiOperation(value = "查看角色资源选项")
    @GetMapping("/admin/role/resources")
    public RestResponse<List<LabelOptionDTO>> listResourceOption() {
        return RestResponse.ok(sysResourceService.listResourceOption());
    }

}

